package demo.java.lang.enumeration;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

/**
 * 扑克牌
 *
 */
public class Card {

    private static Map<Suit, Map<Rank, Card>> table = new EnumMap<Suit, Map<Rank, Card>>(Suit.class);

    static {
        for (Suit suit : Suit.values()) {
            Map<Rank, Card> suitTable = new EnumMap<Rank, Card>(Rank.class);
            for (Rank rank : Rank.values()) {
                suitTable.put(rank, new Card(rank, suit));
            }
            table.put(suit, suitTable);
        }
    }

    public static Card valueOf(Rank rank, Suit suit) {
        return table.get(suit).get(rank);
    }

    public static ArrayList<Card> deal(List<Card> deck, int n) {
        int deckSize = deck.size();
        List<Card> handView = deck.subList(deckSize - n, deckSize);
        ArrayList<Card> hand = new ArrayList<Card>(handView);
        handView.clear();
        return hand;
    }

    /**
     * 数字
     */
    public enum Rank {
        DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
    }

    /**
     * 花色
     */
    public enum Suit {
        CLUBS, DIAMONDS, HEARTS, SPADES
    }

    private final Rank rank;
    private final Suit suit;

    private Card(Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public Rank rank() {
        return rank;
    }

    public Suit suit() {
        return suit;
    }

    @Override
    public String toString() {
        return rank + " of " + suit;
    }

    private static final List<Card> protoDeck = new ArrayList<Card>();

    // Initialize prototype deck
    static {
        for (Suit suit : Suit.values()) {
            for (Rank rank : Rank.values()) {
                protoDeck.add(new Card(rank, suit));
            }
        }

    }

    public static ArrayList<Card> newDeck() {
        return new ArrayList<Card>(protoDeck); // Return copy of prototype deck
    }
}
